package com.effectivejava.composition;

import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

/**
 * This class is known as a "wrapper class" because each InstrumentedSet
 * instance contains another set instance. This is also known as the
 * decarator pattern, because the InstrumentedSet "decorate" the Set by
 * adding a new InstrumentedSet.
 * 
 * @author brian_zhang
 *
 * @param <E>
 */
public class InstrumentedSet<E> extends ForwardingSet<E> {
	
	private int addCount;

	public InstrumentedSet(Set<E> s) {
		super(s);
	}

	@Override public boolean add(E e) {
		addCount++;
		return super.add(e);
	}

	@Override public boolean addAll(Collection<? extends E> c) {
		addCount += c.size();
		return super.addAll(c);
	}

	public int getAddCount() {
		return addCount;
	}
	
	public static void main(String[] args){
		Set<Date> test1 = new InstrumentedSet<Date>(new TreeSet<Date>());
		Set<String> test2 = new InstrumentedSet<String>(new HashSet<String>());
	}
}
